library(ggplot2)
library(dplyr)
library(plotly)
library(igraph)
library(ggdendro)
library(ggmap)
library(gganimate)Other visualization techniques
Overview
We will cover
- visualizing data with geographical information
- visualizing network data (similar to python’s
networkx) - visualizing tree-like structures
- dynamic visualization
ggmap
#get the stamen map of pullman, wa
map <- get_stamenmap(bbox = c(-117.1670, 46.7350, -117.12, 46.75), zoom = 15, maptype = "toner-lite")ℹ Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under ODbL.
ggmap(map)igraph
The dataset
- the dataset contains relationships among several media outlets, if one is referenced by
mentionorhyperlinkthen they are “connected”
nodes <- read.csv(file ='data/Dataset1-Media-Example-NODES.csv', header = T, as.is = T)
edges <- read.csv("data/Dataset1-Media-Example-EDGES.csv", header = T, as.is = T)
head(nodes) id media media.type type.label audience.size
1 s01 NY Times 1 Newspaper 20
2 s02 Washington Post 1 Newspaper 25
3 s03 Wall Street Journal 1 Newspaper 30
4 s04 USA Today 1 Newspaper 32
5 s05 LA Times 1 Newspaper 20
6 s06 New York Post 1 Newspaper 50
Create igraph object
directed = Tcreates a digraph- note
d = links, vertices = nodesand “media” innet
net <- graph_from_data_frame(d = edges, vertices = nodes, directed = T)
netIGRAPH e677085 DNW- 17 49 --
+ attr: name (v/c), media (v/c), media.type (v/n), type.label (v/c),
| audience.size (v/n), type (e/c), weight (e/n)
+ edges from e677085 (vertex names):
[1] s01->s02 s01->s03 s01->s04 s01->s15 s02->s01 s02->s03 s02->s09 s02->s10
[9] s03->s01 s03->s04 s03->s05 s03->s08 s03->s10 s03->s11 s03->s12 s04->s03
[17] s04->s06 s04->s11 s04->s12 s04->s17 s05->s01 s05->s02 s05->s09 s05->s15
[25] s06->s06 s06->s16 s06->s17 s07->s03 s07->s08 s07->s10 s07->s14 s08->s03
[33] s08->s07 s08->s09 s09->s10 s10->s03 s12->s06 s12->s13 s12->s14 s13->s12
[41] s13->s17 s14->s11 s14->s13 s15->s01 s15->s04 s15->s06 s16->s06 s16->s17
[49] s17->s04
Create graph
Two sub-networks; wall street journal (has the most edges)
Code
#remove multiple edges and loops
net1 <- simplify(net, remove.multiple = T, remove.loops = T)
plot(net1, edge.arrow.size = 0.4, edge.color = "grey", vertex.label = V(net)$media, vertex.label.color = "black")ggdendro
head(USArrests) Murder Assault UrbanPop Rape
Alabama 13.2 236 58 21.2
Alaska 10.0 263 48 44.5
Arizona 8.1 294 80 31.0
Arkansas 8.8 190 50 19.5
California 9.0 276 91 40.6
Colorado 7.9 204 78 38.7
Code
hc <- hclust(dist(USArrests), "ave") #clustering
ggdendrogram(hc, rotate = T) #rotate the dendrogram by 90 degreesDynamic visualizations
- Interactive visualizations allow a user to interact with a plot
- usually presents relationships between specific features of data as other features change
plotlycreates interactive plots andgganimatecreates animated plots
plotly
Important
In order to take a screenshot of a plotly plot, we use the library webshot and phantomjs
install.packages("webshot")
webshot::install_phantomjs()Plotting
Example 1
Code
p <- plot_ly(iris, x = ~Petal.Length, y = ~Petal.Width, color = ~Species, type = "scatter", mode = "markers") %>%
layout(xaxis = list(title = "Petal Length"), yaxis = list(title = "Petal Width"))
library(shiny)
div(p)Example 2
Code
p1 <- plot_ly(midwest, x = ~percollege, color = ~state, type = "box") %>%
layout(xaxis = list(title = "percent of educated population"))
p1Screenshot
library(htmlwidgets)
htmlwidgets::saveWidget(p1, "p1.html", selfcontained = F)gganimate
Code
library(gapminder)
library(gganimate)
library(gifski)
ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() +
facet_wrap(~continent) +
# Here comes the gganimate specific bits
labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +
transition_time(year) +
ease_aes('linear')